package org.jjche.sys.modules.fillrule.rest;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.jjche.common.base.BaseDTO;
import org.jjche.common.enums.LogCategoryType;
import org.jjche.common.enums.LogType;
import org.jjche.common.param.MyPage;
import org.jjche.common.param.PageParam;
import org.jjche.common.wrapper.response.R;
import org.jjche.core.base.BaseController;
import org.jjche.log.biz.starter.annotation.LogRecord;
import org.jjche.sys.modules.fillrule.api.SysFillRuleApi;
import org.jjche.sys.modules.fillrule.dto.SysFillRuleDTO;
import org.jjche.sys.modules.fillrule.dto.SysFillRuleQueryCriteriaDTO;
import org.jjche.sys.modules.fillrule.service.SysFillRuleService;
import org.jjche.sys.modules.fillrule.vo.SysFillRuleVO;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 系统编码规则 控制器
 * </p>
 *
 * @author miaoyj
 * @since 2024-05-11
 */
@Api(tags = "系统编码规则")
@ApiSupport(order = 1, author = "miaoyj")
@RestController
@RequestMapping("fill_rules")
@RequiredArgsConstructor
public class SysFillRuleController extends BaseController {

    private final SysFillRuleService sysFillRuleService;
    private final SysFillRuleApi fillRuleApi;

    @PostMapping
    @ApiOperation(value = "系统编码规则-新增")
    @ApiOperationSupport(ignoreParameters = {"id"})
    @PreAuthorize("@el.check('sys_fill_rule:add')")
    @LogRecord(
            value = "新增",
            category = LogCategoryType.OPERATING,
            type = LogType.ADD, module = "系统编码规则", bizNo = "{{#_ret.data}}"
    )
    public R<Long> create(@Validated @Valid @RequestBody SysFillRuleDTO dto) {
        return R.ok(sysFillRuleService.save(dto));
    }

    @DeleteMapping
    @ApiOperation(value = "系统编码规则-删除")
    @PreAuthorize("@el.check('sys_fill_rule:del')")
    @LogRecord(value = "删除", category = LogCategoryType.OPERATING,
            type = LogType.DELETE, module = "系统编码规则", bizNo = "{{#ids}}",
            detail = "{API_MODEL{#_oldObj}} {SYS_FILL_RULE_DIFF_OLD_BY_ID{#ids}}"
    )
    public R delete(@RequestBody List<Long> ids) {
        sysFillRuleService.delete(ids);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "系统编码规则-修改")
    @PreAuthorize("@el.check('sys_fill_rule:edit')")
    @LogRecord(
            value = "修改", category = LogCategoryType.OPERATING,
            type = LogType.UPDATE, module = "系统编码规则", bizNo = "{{#dto.id}}",
            detail = "{_DIFF{#dto}} {SYS_FILL_RULE_DIFF_OLD_BY_ID{#dto.id}}"
    )
    public R update(@Validated(BaseDTO.Update.class)
                    @Valid @RequestBody SysFillRuleDTO dto) {
        sysFillRuleService.update(dto);
        return R.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "系统编码规则-查询单个")
    @PreAuthorize("@el.check('sys_fill_rule:list')")
    public R<SysFillRuleVO> getById(@PathVariable Long id) {
        return R.ok(this.sysFillRuleService.getVoById(id));
    }

    @ApiOperation(value = "系统编码规则-导出")
    @GetMapping(value = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @PreAuthorize("@el.check('sys_fill_rule:list')")
    public void download(@Validated SysFillRuleQueryCriteriaDTO criteria) {
        sysFillRuleService.download(criteria);
    }

    @GetMapping
    @ApiOperation(value = "系统编码规则-列表")
    @PreAuthorize("@el.check('sys_fill_rule:list')")
    public R<MyPage<SysFillRuleVO>> pageQuery(PageParam page,
                                              @Validated SysFillRuleQueryCriteriaDTO query) {
        return R.ok(sysFillRuleService.page(page, query));
    }

    /**
     * 测试 ruleCode
     *
     * @param ruleCode
     * @return
     */
    @GetMapping(value = "/test")
    @ApiOperation(value = "系统编码规则-测试")
    public R<String> testFillRule(String ruleCode) {
        String result = fillRuleApi.executeRule(ruleCode, null);
        return R.ok(result);
    }

}
